package com.css.fxfzypg.modules.suggestinfo.manage.annualriskManage.repository.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.modules.suggestinfo.manage.annualriskManage.repository.AnnualRiskManageRepository;
import com.css.fxfzypg.modules.suggestinfo.manage.annualriskManage.repository.entity.AnnualRiskManageEntity;
import com.css.fxfzypg.modules.suggestinfo.manage.disasterscenarioManage.repository.entity.DisasterScenarioManageEntity;
import com.css.fxfzypg.modules.suggestinfo.manage.vo.ParamVo;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AnnualRiskManageRepositoryImpl implements AnnualRiskManageRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public boolean save(AnnualRiskManageEntity annualRiskManageEntity) {
        String sql = "insert into ypg_annual_risk_manage values(:id,:province,:provinceId,:city,:cityId,:area,:areaId," +
                ":building,:disasterHiddenDanger,:lifelineSystem,:majorInfrastructure,:otherAnnualDisaster," +
                ":delFlag,:createId,:createTime,:updateId,:updateTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(annualRiskManageEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public AnnualRiskManageEntity getDataById(String id) {
        String sql = "select * from ypg_annual_risk_manage where  del_flag = '0' and id= ?";
        try {
            AnnualRiskManageEntity annualRiskManageEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AnnualRiskManageEntity.class),id);
            return annualRiskManageEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public Map<String, Object> getDataList(ParamVo paramVo, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select id,province,province_id,city,city_id,area,area_id from ypg_annual_risk_manage ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' ");
        if(!PlatformObjectUtils.isEmpty(paramVo.getProvinceId())){
            whereSql.append(" and province_id = '"+ paramVo.getProvinceId()+"\'");
        }
        if(!PlatformObjectUtils.isEmpty(paramVo.getCityId())){
            whereSql.append(" and city_id = '"+ paramVo.getCityId()+"\'");
        }
        if(!PlatformObjectUtils.isEmpty(paramVo.getAreaId())){
            whereSql.append(" and area_id = '"+ paramVo.getAreaId()+"\'");
        }
        int firstIndex=(curPage-1) * pageSize;
        int lastIndex=pageSize;
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        StringBuilder sql1 = sql.append(whereSql);
        Integer total = jdbcTemplate.query(sql1.toString(), new BeanPropertyRowMapper<>(ParamVo.class)).size();
        List<ParamVo> annualRiskManageEntityList = jdbcTemplate.query(sql1.append(orderSql).append(limitSql).toString(), new BeanPropertyRowMapper<>(ParamVo.class));
        Map<String,Object> map=new HashMap();
        map.put("total",total);
        map.put("rows",annualRiskManageEntityList);
        return map;
    }

    @Override
    public boolean update(AnnualRiskManageEntity annualRiskManageEntity) {
        String sql = "update ypg_annual_risk_manage set province=:province,province_id=:provinceId,city=:city,city_id=:cityId,area=:area,area_id=:areaId," +
                "building=:building,disaster_hidden_danger=:disasterHiddenDanger,lifeline_system=:lifelineSystem,major_infrastructure=:majorInfrastructure,other_Annual_Disaster=:otherAnnualDisaster," +
                "update_id=:updateId,update_time=:updateTime where id = '"+annualRiskManageEntity.getId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(annualRiskManageEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public int getCount(String provinceId, String cityId, String areaId) {
        String sql = "select count(*) from ypg_annual_risk_manage where del_flag = '0' and province_id = ? and city_id = ? and area_id =? ";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, provinceId,cityId,areaId);
        return count;
    }

    @Override
    public boolean delete(String id) {
        String sql = "update ypg_annual_risk_manage set del_flag = '1' where id = ?";
        return jdbcTemplate.update(sql,id) > 0;
    }

    @Override
    public String getId(String provinceId, String cityId, String areaId) {
        String sql = "select id from ypg_annual_risk_manage where del_flag = '0' and province_id = ? and city_id = ? and area_id =? ";
        String id = jdbcTemplate.queryForObject(sql, String.class, provinceId,cityId,areaId);
        return id;
    }

    @Override
    public AnnualRiskManageEntity getAnnualRiskManageData(ParamsVo paramsVo) {
        try {
            String sql = "select * from ypg_annual_risk_manage where  del_flag = '0' and province_id = ? and city_id = ? and area_id =? ";
            AnnualRiskManageEntity annualRiskManageEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AnnualRiskManageEntity.class),paramsVo.getProvinceId(),paramsVo.getCityId(),paramsVo.getAreaId());
            return annualRiskManageEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public List<AnnualRiskManageEntity> getAnnualRiskManageAreaData(ParamsVo paramsVo) {
        StringBuilder sql = new StringBuilder("select * from ypg_annual_risk_manage ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and province_id = '"+paramsVo.getProvinceId()+"' and city_id = '"+paramsVo.getCityId()+"'");
        List<AnnualRiskManageEntity> annualRiskManageEntities = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(AnnualRiskManageEntity.class));
        return annualRiskManageEntities;
    }

    @Override
    public AnnualRiskManageEntity getByProvinceCityCounty(String provinceId, String cityId, String areaId) {
        try {
            String sql = "select * from ypg_annual_risk_manage where province_id = '" + provinceId + "' and city_id = '" + cityId + "' and area_id = '" + areaId + "' and del_flag = '0' order by create_time desc limit 1 offset 0";
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AnnualRiskManageEntity.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public Integer countByCounty(String p, String city, String county) {
        String sql = String.format("select count(*) from ypg_annual_risk_manage where province = '%s' and city = '%s' and area = '%s' and del_flag = '0'", p, city, county);
        return jdbcTemplate.queryForObject(sql, Integer.class);
    }
}
